home *** CD-ROM | disk | FTP | other *** search
- Path: news.infi.net!usenet
- From: nngis@norfolk.infi.net (Greg DiGiorgio)
- Newsgroups: comp.lang.c
- Subject: Re: Finding a prime number
- Date: 25 Jan 1996 20:33:00 GMT
- Organization: Customer of InfiNet
- Message-ID: <4e8pds$4va@nw002.infi.net>
- References: <4e875s$nqk@reader2.ix.netcom.com>
- Reply-To: nngis@norfolk.infi.net
- NNTP-Posting-Host: h-standbyme.norfolk.infi.net
- Mime-Version: 1.0
- X-Newsreader: WinVN 0.99.3
-
- In article <4e875s$nqk@reader2.ix.netcom.com>, advtr@ix.netcom.co says...
-
- There's more to it than you are coding. After your code, I have placed a
- sample program to calculate if a number is a prime number and, if not,
- then print out all prime factors of that number. This program does not
- however print out all prime numbers from 1..n. It could be easily
- modified to do that, though.
- Hope this helps,
- Greg DiGiorgio
-
- >
- >I need to write a function that will find wether or not a number is
- >prime. I can come close but I get numbers that are not prime with the
- >prime numbers.
- >Here is the function I wrote. Any help would be great. Thanks Ken(A
- >begining C programmer)
-
- ... snipped ...
-
- /************************************************************************
- **
- PRIME.C
-
- *************************************************************************
- */
-
- #include <stdio.h>
-
- int is_prime(int n) { /* Tell me if a number is a prime number... */
-
- int i; /* Loop counter */
-
- for (i=2; i<n; i++) {
- if ((n % i)==0) /* Number, n, is evenly divisible
- */
- return(0); /* by i. Therefore it can not be
- */
- /* be a prime number. So quit and
- */
- } /* tell caller it is not prime.
- */
-
- return(1); /* If we get here, the number is
- */
- /* indeed a prime number, so tell
- */
- } /* caller so.
- */
-
- void calc_prime_factor(int n) { /* Print out all prime factors for a
- number */
-
- int i; /* Loop counter */
-
- printf("The prime factors of %d are: ",n); /* First part of
- output */
-
- for (i=2; i<n; i++) {
-
- /*------------------------------*/
- if (is_prime(i)) /* Is 'i' a prime number ?
- */
-
- /*------------------------------*/
- if ((n % i)==0) /* Is 'n' evenly divisible by
- */
- /* this prime number? If so,
- */
- /* we have found a prime
- factor.*/
-
- /*------------------------------*/
- printf("%d and ",i); /* output prime factor
- */
- }
- printf("\n"); /* Last part of output -> newline character.
- */
- }
-
- int main (void) {
- int n;
- char c;
-
- /********************************************************
- * Force user to enter a number between 1 and 100 *
- ********************************************************/
- ANOTHER:
- n=0; /* Force us thru loop at least once */
- while (n<1 || n>100) {
- printf("Enter a positive integer between 1 and 100: ");
- fflush(stdin);
- scanf("%d",&n);
- if (n<1)
- printf("Number is too small.\n");
- else if (n>100)
- printf("Number is too large.\n");
- }
-
- /********************************************************
- * We've gotten the number, now see if the number is *
- * a PRIME number or calculate its prime factors. *
- ********************************************************/
- if (is_prime(n))
-
- printf("The number %d is a prime number.\n",n);
-
- else { /* Not a prime, so calc its prime factors... */
- calc_prime_factor(n);
- }
-
- /********************************************************
- * Does user want to do it again? *
- ********************************************************/
- printf("Another number? (Y/N) ");
- fflush(stdin);
- scanf("%c",&c);
- if (c=='Y' || c=='y')
- goto ANOTHER;
- else
- printf("Goodbye!\n");
- return(0);
- }
-
-
-